package com.health.hfm.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.health.core.annotation.HealthAop;
import com.health.core.constant.ServiceConstants;
import com.health.core.support.HealthThreadLocal;
import com.health.core.util.R;
import com.health.hfm.sys.config.HfmEnterpriseConfigService;
import com.health.hfm.mapper.UserDocsTypeMapper;
import com.health.hfm.pojo.UserDocsTypePO;
import com.health.hfm.service.UserDocsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "慢病档案管理")
@RequestMapping(ServiceConstants.HEALTH_FILE_MEASURE_URL_PR+"/chronic")
@RestController
public class ChronicDiseaseController {

    @Autowired
    UserDocsTypeService userDocsTypeService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserDocsTypeMapper userDocsTypeMapper;

    @Autowired
    HfmEnterpriseConfigService enterpriseConfigService;

    @ApiOperation("获取档案类型下拉框")
    @GetMapping("getDownBox")
    @HealthAop
    @DS("#header.enterprise")
//    @DSTransactional
    public R getDownBox(HttpServletRequest request) {
        Map<String, DataSource> dataSourceMap = HfmEnterpriseConfigService.dataSourceMap;
        String dataSourceKey = HealthThreadLocal.getDataSourceKey();
        String gatewayKey = request.getHeader("gatewayKey");
        Map<String, String> files = userDocsTypeService.list(new LambdaQueryWrapper<UserDocsTypePO>()
                .eq(UserDocsTypePO::getSource, "FILES")
                .groupBy(UserDocsTypePO::getKey))
                .stream().collect(Collectors.toMap(
                        UserDocsTypePO::getKey, UserDocsTypePO::getName));
        return R.ok(files);
    }


}
